<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ name }} | Xmind2TestCase Preview</title>
    <link rel="shortcut icon" href="{{ url_for('static',filename='favicon.ico') }}" type="image/x-icon"/>
    <link rel="stylesheet" type="text/css" media="all" href="{{ url_for('static',filename='css/pure-min.css') }}">
    <link rel="stylesheet" type="text/css" media="all" href="{{ url_for('static',filename='css/custom.css') }}">
    <style>
        /* Modal 背景 */
        .modal {
            display: none;
            position: fixed;
            z-index: 2000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
        }
        /* Modal 内容 */
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 300px;
        }
        /* 关闭按钮 */
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }
        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
        }
        /* 修改配置按钮样式 */
        #modifyConfigBtn {
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 1000;
        }
    </style>
</head>
<body>
    <!-- 访问禅道用例导入工具入口，添加 id 便于操作 -->
    <a href="{{ config.zen_tao_importer_url }}" id="zentaoLink" class="pure-button"
   style="position: absolute; top: 50px; right: 70px; z-index: 1000; color: green;">
   访问禅道用例导入工具
    </a>
    <!-- 页面右下角新增修改配置入口 -->
    <a href="javascript:void(0);" id="modifyConfigBtn" class="pure-button">修改配置</a>

    <div class="header">
        <h1>{{ name }} - Preview</h1>
        <h2>TestSuites: {{ suite_count }} / TestCases: {{ suite | length }}
            / <a href="{{ url_for("download_zentao_file", filename=name) }}">Get Zentao CSV</a>
            / <a href="{{ url_for("download_testlink_file", filename=name) }}">Get TestLink XML</a>
            / <a href="{{ url_for("index") }}">Go Back</a>
        </h2>
    </div>
    <table class="pure-table tests-table">
        <thead>
            <tr>
                <th width="5%">#</th>
                <th width="10%">Suite</th>
                <th>Title</th>
                <th width="200px">Attributes</th>
                <th width="23%">Steps</th>
            </tr>
        </thead>
        <tbody>
        {% for test in suite %}
            <tr>
                <td>{{ loop.index }}</td>
                <td>{{ test.suite }}</td>
                <td {% if test.name|length > 100 %}class="long-name"{% endif %}>
                    {{ test.name }}
                    {% if test.name|length > 100 %}
                        <span class="long-name-info">Warn: test name might be too long: {{ test.name|length }}!</span>
                    {% endif %}
                </td>
                <td>
                    <div class="tag-success tooltip">
                        Priority {{ test.importance }}
                        <span class="tooltiptext">Priority {{ test.importance }} </span>
                    </div>
                    {% if test.preconditions %}
                        <div class="pure-button tag-info tooltip">PreCond.
                            <p class="tooltiptext">
                                <b>Preconditions:</b> <br>
                                {{ test.preconditions | replace('\n','<br>') | safe }}
                            </p>
                        </div>
                    {% endif %}
                    {% if test.summary %}
                        <div class="pure-button tag-warn tooltip">Summary
                            <p class="tooltiptext">
                                <b>Summary:</b> <br>
                                {{ test.summary | replace('\n','<br>') | safe }}
                            </p>
                        </div>
                    {% endif %}
                </td>
                <td>
                    {% if test.steps %}
                        <ol>
                        {% for step in test.steps %}
                            <li>{{ step.actions }}
                                {% if step.expectedresults %}
                                    <ul>
                                        <li>{{ step.expectedresults }}</li>
                                    </ul>
                                {% endif %}
                            </li>
                        {% endfor %}
                        </ol>
                    {% endif %}
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    <div class="footer">
        <a href="{{ url_for('static', filename='guide/index.html') }}" target="_blank">User Guide</a> |
        <a href="https://github.com/zhuifengshen/xmind2testcase/issues/new" target="_blank">Report Issue</a> |
        Powered by <a href="https://github.com/zhuifengshen/xmind2testcase" target="_blank">XMind2TestCase</a>
    </div>

    <!-- 弹窗：修改配置 -->
    <div id="configModal" class="modal">
        <div class="modal-content">
            <span class="close">&times;</span>
            <h2>修改配置</h2>
            <label for="zenTaoUrl">禅道用例导入工具默认URL:</label>
            <input type="text" id="zenTaoUrl" name="zenTaoUrl" value="{{ config.zen_tao_importer_url }}" style="width: 100%; margin-bottom: 10px;"/>
            <div style="text-align: right;">
                <button id="resetConfig" class="pure-button" style="margin-right: 10px;">重置</button>
                <button id="saveConfig" class="pure-button">保存</button>
            </div>
        </div>
    </div>

    <script>
        // 获取弹窗和相关按钮元素
        var modal = document.getElementById("configModal");
        var modifyBtn = document.getElementById("modifyConfigBtn");
        var closeBtn = document.getElementsByClassName("close")[0];

        // 点击“修改配置”按钮时显示弹窗
        modifyBtn.onclick = function() {
            modal.style.display = "block";
        };

        // 点击关闭按钮 (×) 关闭弹窗
        closeBtn.onclick = function() {
            modal.style.display = "none";
        };

        // 点击弹窗外区域关闭弹窗
        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        };


        document.getElementById("saveConfig").onclick = function() {
        const newUrl = document.getElementById("zenTaoUrl").value.trim();
        if (!newUrl.startsWith("http://") && !newUrl.startsWith("https://")) {
            alert("请输入完整的URL（包含协议，例如：http://www.baidu.com）");
            return;
        }

        fetch('/save-config', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
                zen_tao_importer_url: newUrl
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data.status === 'success') {
                alert('配置已保存，页面即将刷新');
                location.reload();
            } else {
                alert('保存失败: ' + (data.message || '未知错误'));
            }
        });
    }

    document.getElementById("resetConfig").onclick = function() {
        fetch('/get-config')
            .then(response => response.json())
            .then(config => {
                document.getElementById("zenTaoUrl").value = config.zen_tao_importer_url;
            });
    }
    </script>
</body>
</html>
